/*
day234
2020.4.18
*/
#include<iostream>
#include<cstdlib>
#include<deque>
using namespace std;

struct TreeNode {
    int val;
    TreeNode *left;
    TreeNode *right;
    TreeNode(int x) : val(x), left(NULL), right(NULL) {}
};

class CBTInserter {
	TreeNode *root;
	deque<TreeNode *> que;
public:
    CBTInserter(TreeNode* root) {
        this->root=root;
        this->que.push_back(root);
        while(que.front()->left && que.front()->right)
        {
        	que.push_back(que.front()->left);
        	que.push_back(que.front()->right);
        	que.pop_front();
		}
    }
    
    int insert(int v) {
        TreeNode *node=new TreeNode(v);
        int res=0;
        if(!que.front()->left)
        {
        	que.front()->left=node;
        	res=que.front()->val; 
		}
		else
		{
			que.front()->right=node;
			res=que.front()->val;
			que.push_back(que.front()->left);
			que.push_back(que.front()->right);
			que.pop_front();
		}
		return res;
    }
    
    TreeNode* get_root() {
        return this->root;
    }
};

int main()
{
	return 0;
}
